package com.jtpay.payresulthelper.http.loader;

import com.google.gson.JsonObject;
import com.jtpay.payresulthelper.http.dataStructure.BasicResponse;
import com.jtpay.payresulthelper.http.dataStructure.JsonParams;
import com.jtpay.payresulthelper.http.retrofit.RetrofitServiceManager;
import com.jtpay.payresulthelper.http.service.ApiInterface;
import io.reactivex.Observable;

/**
 * Description:
 * File: ApiLoader.java
 * Author: Administrator
 * Create: 2018/07/30 09:34
 */
public class ApiLoader extends ObjectLoader {
    private ApiInterface apiJsonInterface;
    private ApiInterface apiInterface;

    public ApiLoader() {
        apiJsonInterface = RetrofitServiceManager.getJsonInstance().create(ApiInterface.class);
        apiInterface = RetrofitServiceManager.getInstance().create(ApiInterface.class);
    }

    public ApiLoader(String baseUrl) {
        RetrofitServiceManager manager = RetrofitServiceManager.getJsonInstance();
        manager.setBaseUrl(baseUrl);
        apiJsonInterface = manager.create(ApiInterface.class);
        RetrofitServiceManager manager2 = RetrofitServiceManager.getInstance();
        manager2.setBaseUrl(baseUrl);
        apiInterface = manager2.create(ApiInterface.class);
    }

    public ApiLoader(String baseUrl, long timeout) {
        RetrofitServiceManager manager = RetrofitServiceManager.getJsonInstance();
        manager.setBaseUrl(baseUrl);
        manager.setHttpTimeout(timeout);
        apiJsonInterface = manager.create(ApiInterface.class);
        RetrofitServiceManager manager2 = RetrofitServiceManager.getInstance();
        manager2.setBaseUrl(baseUrl);
        manager2.setHttpTimeout(timeout);
        apiInterface = manager2.create(ApiInterface.class);
    }

    /**
     * post Response JsonObject
     *
     * @param api    String
     * @param params params
     * @return Observable
     */
    public Observable<BasicResponse<JsonObject>> postJsonObserveJsonParam(String api, JsonParams params) {
        return observe(apiJsonInterface.postJsonBody(api, params.buildJsonRequestBody()));
    }

    /**
     * post Response JsonObject
     *
     * @param api    String
     * @param params params
     * @return Observable
     */
    public Observable<BasicResponse<JsonObject>> postJsonObserveDESRequestBody(String api, JsonParams params) {
        return observe(apiJsonInterface.postJsonBody(api, params.getDesRequestBody()));
    }

    /**
     * post Response String
     *
     * @param api    String
     * @param params params
     * @return Observable
     */
    public Observable<String> postStrObserveRequestBody(String api, JsonParams params) {
        return observe(apiInterface.postTextBody(api, params.buildRequestBody()));
    }

    /**
     * get  Response JsonObject
     *
     * @param api    String
     * @param params params
     * @return Observable
     */
    public Observable<BasicResponse<JsonObject>> getJsonObserveJsonParam(String api, JsonParams params) {
        return observe(apiJsonInterface.getJsonMap(api, params));
    }

    /**
     * post Response Json
     *
     * @param api    String
     * @param object object
     * @return Observable
     */
    public Observable<BasicResponse<JsonObject>> postJsonObserveObject(String api, Object object) {
        return observe(apiJsonInterface.postJsonObject(api, object));
    }

    /**
     * post Response Str
     *
     * @param api    String
     * @param object object
     * @return Observable
     */
    public Observable<BasicResponse<String>> postStrObserveObject(String api, Object object) {
        return observe(apiJsonInterface.postStrObject(api, object));
    }

    /**
     * get Response Str
     *
     * @param api    String
     * @param params params
     * @return Observable
     */
    public Observable<BasicResponse<String>> getStrObserveJsonParam(String api, JsonParams params) {
        return observe(apiJsonInterface.getStrMap(api, params));
    }

}
